package com.jiang.daily.Q1277;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/08/20 16:39
 */
class Solution {
    public int countSquares(int[][] matrix) {
        int n = matrix.length;
        int m = matrix[0].length;
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j] == 0) continue;
                if (!(i == 0 || j == 0)) {
                    matrix[i][j] = Math.min(Math.min(matrix[i - 1][j], matrix[i][j - 1]), matrix[i - 1][j - 1]) + 1;
                }
                res += matrix[i][j];
            }
        }
        return res;
    }
}
